package com.pure.common.equator;

import com.alibaba.fastjson.JSON;
import com.github.dadiyang.equator.Equator;
import com.github.dadiyang.equator.FieldBaseEquator;
import com.github.dadiyang.equator.FieldInfo;
import com.github.dadiyang.equator.GetterBaseEquator;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
public class DiffEquator {

    @SneakyThrows
    public static void main(String[] args) {

        Date beforeStartTime = Date.from(LocalDateTime.of(9999, Month.DECEMBER, 31, 12, 0, 0).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更前价格生效时间:{}", beforeStartTime);
        Date beforeEndTime = Date.from(LocalDateTime.of(2023, Month.JANUARY, 7, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更前价格截止时间:{}", beforeEndTime);

        Date afterStartTime = Date.from(LocalDateTime.of(2022, Month.JUNE, 6, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更后价格生效时间:{}", afterStartTime);
        Date afterEndTime = Date.from(LocalDateTime.of(2023, Month.JANUARY, 30, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更后价格截止时间:{}", afterEndTime);


        Date beforeDiscountStartTime = Date.from(LocalDateTime.of(2022, Month.JUNE, 7, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更前折扣生效时间:{}", beforeDiscountStartTime);
        Date beforeDiscountEndTime = Date.from(LocalDateTime.of(2023, Month.JANUARY, 7, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更前折扣截止时间:{}", beforeDiscountEndTime);

        Date afterDiscountStartTime = Date.from(LocalDateTime.of(2022, Month.JUNE, 6, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更后折扣生效时间:{}", afterDiscountStartTime);
        Date afterDiscountEndTime = Date.from(LocalDateTime.of(2023, Month.JANUARY, 30, 16, 20, 55).atZone(ZoneId.systemDefault()).toInstant());
        log.info("变更后折扣截止时间:{}", afterDiscountEndTime);


        ProMsgDiffFieldPojo oldProMsg = new ProMsgDiffFieldPojo();
        oldProMsg.setAnnulUnit("2001");
        oldProMsg.setMatnr("10000001");
        oldProMsg.setMaterialLongName("稳心颗粒-9g*9袋 9G/袋╳9袋/盒 100盒/件");
        oldProMsg.setDeliveryMethod("Z06");
        oldProMsg.setPrice(BigDecimal.valueOf(664.00000D).setScale(5));
        oldProMsg.setStartTime(beforeStartTime);
        oldProMsg.setEndTime(beforeEndTime);
        oldProMsg.setDiscountType(0);
        oldProMsg.setDiscount(BigDecimal.valueOf(2.2D));
        oldProMsg.setDiscountStartTime(beforeDiscountStartTime);
        oldProMsg.setDiscountEndTime(beforeDiscountEndTime);


        ProMsgDiffFieldPojo newProMsg = new ProMsgDiffFieldPojo();

        newProMsg.setAnnulUnit("2002");
        newProMsg.setMatnr("10000031");
        newProMsg.setMaterialLongName("冠心舒通胶囊-0.3g×60粒 0.3g×60粒 200盒/件");
        newProMsg.setDeliveryMethod("Z07");
        newProMsg.setPrice(BigDecimal.valueOf(664).setScale(5));
        newProMsg.setStartTime(afterStartTime);
        newProMsg.setEndTime(afterEndTime);
        newProMsg.setDiscountType(1);
        newProMsg.setDiscount(BigDecimal.valueOf(66));
        newProMsg.setDiscountStartTime(afterDiscountStartTime);
        newProMsg.setDiscountEndTime(afterDiscountEndTime);

        GetterBaseEquator getterBaseEquator = new GetterBaseEquator();
        Equator fieldBaseEquator = new FieldBaseEquator();
        List<FieldInfo> diffFields = fieldBaseEquator.getDiffFields(oldProMsg, newProMsg); // 差异

        log.info("diff fields {}", JSON.toJSONString(diffFields));

        Date originalEndDate = DateUtils.parseDate("9999-12-31 12:00:00", "yyyy-MM-dd HH:mm:ss");
        if (originalEndDate.compareTo(beforeStartTime) == 0) {
            log.info("original end date = {}", originalEndDate);
        }

    }

}
